﻿@inject IDataCacheService _dataCacheService

<div>
    @foreach (var item in FixedModel)
    {
        <MChip Class="ma-1" OnClick="()=>OnClick(item)" Small Color="primary"
               Outlined="!InputData.Any(s=>s.IsSame(item))">
            @item.Note
        </MChip>
    }


    @if (customTime != null)
    {
        <MChip Class="ma-1" OnClick="OnClean" Small Color="primary">
            @(customTime.AfterTime.ToString("yyyy-MM-dd") + " ~ " + customTime.BeforeTime.ToString("yyyy-MM-dd"))
        </MChip>
    }
    else
    {
        <MChip Class="ma-1" OnClick="OnAdd" Small Color="primary">
            <MIcon Left Small>
                mdi-plus
            </MIcon>
            自定义时间段
        </MChip>
    }
</div>

<MDialog @bind-Value="dialog"
         Persistent
         MaxWidth="400">

    <MDatePicker @bind-Value="_dates" Locale="zh-CN"
                 Range Scrollable>
        <MButton Color="primary" OnClick="ModalCancel">
            取消
         </MButton>
         <MSpacer></MSpacer>
        <MButton Rounded Color="primary" OnClick="ModalOK">
            确定
        </MButton>
    </MDatePicker>
</MDialog>
@code {
    [Parameter]
    public List<SearchTimeModel> InputData { get; set; }
    [Parameter]
    public List<SearchTimeModel> FixedModel { get; set; }
    [Parameter]
    public EventCallback OnChanged { get; set; }

    SearchTimeModel customTime = null;
    bool dialog = false;
    private List<DateOnly> _predates = new List<DateOnly>
    {
         DateOnly.FromDateTime(DateTime.Now.ToCstTime().AddDays(-10)),
         DateOnly.FromDateTime(DateTime.Now.ToCstTime()),
    };
    private List<DateOnly> _dates = new List<DateOnly>
    {
         DateOnly.FromDateTime(DateTime.Now.ToCstTime().AddDays(-10)),
         DateOnly.FromDateTime(DateTime.Now.ToCstTime()),
    };

    public async Task OnClick(SearchTimeModel model)
    {
        if (InputData.Any(s => s.IsSame(model)))
        {
            InputData.RemoveAll(s => s.IsSame(model));
        }
        else
        {
            InputData.Add(model);
        }
        StateHasChanged();
        await OnChanged.InvokeAsync();
    }

    public void OnAdd()
    {
        dialog = true;
    }

    public async Task OnClean()
    {
        customTime = null;
        InputData.Remove(customTime);
        await OnChanged.InvokeAsync();
    }


    public void ModalCancel()
    {
        dialog = false;
        _dates = _predates;

    }

    public async Task ModalOK()
    {
        dialog = false;

        if (customTime != null)
        {
            InputData.Remove(customTime);
        }

        customTime = new SearchTimeModel
        {
            BeforeTime = _dates[1].ToDateTime(TimeOnly.MinValue),
            AfterTime = _dates[0].ToDateTime(TimeOnly.MinValue),
            Note = string.Join(" ~ ", _dates.Select(date => date.ToString("yyyy-MM-dd")))
        };
        InputData.Add(customTime);

        StateHasChanged();
        await OnChanged.InvokeAsync();
    }
}
